package com.iyoudoock.citymine.activity.user;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.iyoudoock.citymine.BaseActivity;
import com.iyoudoock.citymine.R;
import com.iyoudoock.citymine.constants.HttpUtils;
import com.iyoudoock.citymine.constants.Key;
import com.iyoudoock.citymine.constants.Value;
import com.kvin.toolkit.utils.JumpUtils;
import com.kvin.toolkit.utils.ToastUtils;
import com.kvin.toolkit.utils.http.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ResetPswActivity extends BaseActivity {

//    @BindView(R.id.old_psw_et)
//    EditText oldPswEt;
    @BindView(R.id.new_psw_et)
    EditText newPswEt;
    @BindView(R.id.confirm_psw_et)
    EditText confirmPswEt;
    @BindView(R.id.confirm_btn)
    Button confirmBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_psw);
        ButterKnife.bind(this);
    }

    @Override
    public void initNav() {
        setTitle(getString(R.string.nav_reset_psw));
    }

    @Override
    public void init() {
        mJsonUtils = JsonUtils.getInstance();
        this.confirmBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.confirm_btn:
                updatePsw();
                break;
        }
    }

    /**
     * submit update
     */
    private void updatePsw() {
        //String storePsw = mPersistentUtils.readString(Key.User.PASSWORD);
        phoneNo = getIntent().getStringExtra(Key.User.TELEPHONE);
//        userPsw = oldPswEt.getText().toString();
        String newPsw = newPswEt.getText().toString();
        String confirmPsw = confirmPswEt.getText().toString();
        if (TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(confirmPsw)) {
            ToastUtils.simpleToast(getActivity(), R.string.tip_psw_is_null);
            return;
        }
        if (newPsw.equals(userPsw)) {
            ToastUtils.simpleToast(getActivity(), R.string.tip_old_same_new);
            return;
        }
        if (!newPsw.equals(confirmPsw)) {
            ToastUtils.simpleToast(getActivity(), R.string.tip_psw_unequal);
            return;
        }

        params = HttpUtils.resetPsd(phoneNo, newPsw);
        mJsonUtils.post(HttpUtils.RESET_PSW_URL, params, this, Value.Flag.RESET_PSW);
    }


    @Override
    public void onSuccess(JSONObject response, String flag) {
        super.onSuccess(response, flag);
        if (response == null) return;
        try {
            state = response.getInt(Key.Tag.STATE);
            if (flag.equals(Value.Flag.RESET_PSW)) {
                if (state == Value.Response.SUCCESS) {
                    ToastUtils.simpleToast(getActivity(), R.string.tip_update_psw_success);
                    JumpUtils.simpleJump(getActivity(), LoginActivity.class, true);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}
